home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11891 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.0 KB  |  194 lines

  1. Path: pop.gnn.com!HoangTQ
  2. From: Tuyen Hoang <HoangTQ@gnn.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need Help With CSC Homework
  5. Date: Sat, 16 Mar 1996 17:25:23
  6. Organization: GNN
  7. Message-ID: <4iff26$p2i@news-e2b.gnn.com>
  8. References: <4g0qun$jp@news.nevada.edu>
  9. NNTP-Posting-Host: www-41-87.gnn.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset="us-ascii"
  12. X-GNN-NewsServer-Posting-Date: 16 Mar 1996 22:24:06 GMT
  13. X-Mailer: GNNmessenger 1.2
  14.  
  15.  
  16.  
  17. pfun.cpp
  18.  
  19. //*************************************************
  20. //**             ROMANFUN.CPP                    **
  21. //*************************************************
  22. //** This program will add, subtract, multiply,  **
  23. //** and divide roman numbers. The numbers are   **
  24. //** assumed to be less than 20 characters long. **
  25. //** The roman numbers should not be inputed in  **
  26. //** subtraction form. Examples - IV=6 IIII=4    **
  27. //*************************************************
  28. #include <iostream.h>   //for cin and cout
  29. #include "p.h"      //has typedef and prototypes
  30. istream& operator>>(istream& in,Romantype& x)
  31. {
  32. in>>x.Roman;
  33. return in;
  34. }
  35.  
  36. ostream& operator<<(ostream& out,Romantype x)
  37. {
  38. out<<x.Roman;
  39. return out;
  40. }
  41.  
  42. Romantype::Romantype()
  43. {
  44. int i;
  45. for(i=1;i<MAXLENGTH;i++)
  46. {
  47. Roman[i]=' ';
  48. }
  49.  
  50. //and Roman[MAXLENGTH]???
  51. }
  52.  
  53. //*************************************************
  54. //**                 ADD FUNCTION                **
  55. //*************************************************
  56. //** This function adds two roman numbers. It    **
  57. //** requires a character array and two roman    **
  58. //** numbers.                                    **
  59. //*************************************************
  60. void Romantype::operator+(Romantype numbertwo)
  61. {
  62. int numbero;
  63. int numbert;
  64. int answer;
  65. Romantype answerr;
  66. numbero=RomanToDecimal(Roman);
  67. numbert=RomanToDecimal(numbertwo.Roman);
  68. answer=numbero+numbert;
  69. DecimalToRoman(answerr.Roman,answer);
  70. cout<<answerr.Roman;
  71. }
  72.  
  73. //*************************************************
  74. //**     ROMAN TO DECIMAL FUNCTION               **
  75. //*************************************************
  76. //** This function converts a roman number to a  **
  77. //** decimal number. It requires the character   **
  78. //** array and the number of characters in the   **
  79. //** array.                                      **
  80. //*************************************************
  81. int Romantype::RomanToDecimal(char[MAXLENGTH])
  82. {
  83. int number=0;               //value of number intialized to zero
  84. int i;                      //used in for loop
  85. for(i=1;i<MAXLENGTH;i++)          //until end of array add values of letters
  86. {
  87. switch(Roman[i])
  88. {
  89. case 'I':number=number+1;break;
  90. case 'V':number=number+5;break;
  91. case 'X':number=number+10;break;
  92. case 'L':number=number+50;break;
  93. case 'C':number=number+100;break;
  94. case 'D':number=number+500;break;
  95. case 'M':number=number+1000;break;
  96. }
  97. }
  98. return number;    //return decimal value to where the function was called
  99. }
  100.  
  101. //*************************************************
  102. //**     DECIMAL TO ROMAN FUNCTION               **
  103. //*************************************************
  104. //** This function converts a decimal number to  **
  105. //** a roman number. It requires a character     **
  106. //** array and the value of the decimal number.  **
  107. //*************************************************
  108. void Romantype::DecimalToRoman(char[MAXLENGTH]/ *??? */ ,int answer)
  109.                                               ^^^^name of param.?   
  110.                                ^^^^ this is a char* or a Romantype object  
  111. {
  112. int i=1;           //used in for loop
  113. while(answer-1000>=0) //convert to roman by storing letters in array
  114. {
  115. answerr.Roman[i]='M'; 
  116. //^^^^^^Where does the answerr (Romantype object)   come from???    
  117. answer=answer-1000;
  118. i++;
  119. }
  120. while(answer-500>=0)
  121. {
  122. answerr.Roman[i]='D';
  123. answer=answer-500;
  124. i++;
  125. }
  126. while(answer-100>=0)
  127. {
  128. answerr.Roman[i]='C';
  129. answer=answer-100;
  130. i++;
  131. }
  132. while(answer-50>=0)
  133. {
  134. answerr.Roman[i]='L';
  135. answer=answer-50;
  136. i++;
  137. }
  138. while(answer-10>=0)
  139. {
  140. answerr.Roman[i]='X';
  141. answer=answer-10;
  142. i++;
  143. }
  144. while(answer-5>=0)
  145. {
  146. answerr.Roman[i]='V';
  147. answer=answer-5;
  148. i++;
  149. }
  150. while(answer-1>=0)
  151. {
  152. answerr.Roman[i]='I';
  153. answer=answer-1;
  154. i++;
  155. }
  156. }
  157.  
  158.  
  159. //Please review on 
  160.   _function_prototype_ and _parameters_passing_
  161.   member function .
  162.  
  163. //You *miss* some important points
  164.  
  165.     1. none static member function has a hidden *this* pointer. That is
  166.          this->Roman[i] .... so and so ...
  167.       or just
  168.          Roman[i] .... so and so ... ( better )
  169.  
  170.     2. Passing a variable in a function call *does not* mean you can use 
  171. that variable in the function definition. 
  172.   i.e.
  173.    you call the function and pass 'answerr.Roman' as a parameter
  174. DecimalToRoman(answerr.Roman,answer);
  175.    and then you want to use 'answerr.Roman' in side the function    
  176.  
  177. BIG MISTAKE
  178.  
  179.     3. You obmiss the parameter name in the function definition. Is that 
  180. your intense not to use that parameter? If I'm right, you *must* give it a 
  181. name and then use that name in side the function definition instead of 
  182. 'answerr.Roman'
  183.   Please review on function prototype and function definition: they have 
  184. different meaning and syntax
  185.   
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.